package com.zhangzhan.gulimallproduct.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.Query;
import com.zhangzhan.gulimallproduct.dao.BrandDao;
import com.zhangzhan.gulimallproduct.entity.BrandEntity;
import com.zhangzhan.gulimallproduct.service.BrandService;
import com.zhangzhan.gulimallproduct.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key = (String) params.get("key");
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                Wrappers.<BrandEntity>lambdaQuery()
                        .eq(StrUtil.isNotBlank(key), BrandEntity::getBrandId, key)
                        .or().like(StrUtil.isNotBlank(key), BrandEntity::getName, key)
        );

        return new PageUtils(page);
    }

    @Override
    public void updateByIdAndintermediateTable(BrandEntity brand) {
        this.updateById(brand);

        categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());

    }

}